<html>
<head><meta charset="utf-8"><title>Why are non public items monomorphization roots? · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20are.20non.20public.20items.20monomorphization.20roots.3F.html">Why are non public items monomorphization roots?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="232910502"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20are%20non%20public%20items%20monomorphization%20roots%3F/near/232910502" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20are.20non.20public.20items.20monomorphization.20roots.3F.html#232910502">(Apr 02 2021 at 15:15)</a>:</h4>
<p>I just noticed the following in <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_mir/monomorphize/collector/index.html#discovering-roots">https://doc.rust-lang.org/nightly/nightly-rustc/rustc_mir/monomorphize/collector/index.html#discovering-roots</a>:</p>
<blockquote>
<p>The roots of the mono item graph correspond to the non-generic syntactic items in the source code. We find them by walking the HIR of the crate, and whenever we hit upon a function, method, or static item, we create a mono item consisting of the items DefId and, since we only consider non-generic items, an empty type-substitution set.</p>
</blockquote>
<p>From this wording, it sounds like if I have a crate with just <code>fn foo() {}</code>, then since <code>foo</code> is not generic it will act as a "root" of the monomorphization collector. But this function cannot be reached by any other function in the crate, nor by any dependent crate, and it's not public, so I would think that we could just skip monomorphization of this function.</p>



<a name="232911118"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20are%20non%20public%20items%20monomorphization%20roots%3F/near/232911118" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20are.20non.20public.20items.20monomorphization.20roots.3F.html#232911118">(Apr 02 2021 at 15:19)</a>:</h4>
<p>Actually, I just tested this:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">foo</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="kc">true</span><span class="w"> </span><span class="p">{</span><span class="n">foo</span>::<span class="o">&lt;</span><span class="nb">Option</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;&gt;</span><span class="p">()}</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="k">fn</span> <span class="nf">bar</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">foo</span>::<span class="o">&lt;</span><span class="p">()</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>This program compiles fine, so it seems that <code>bar</code> is not actually a root and the docs are incorrect.</p>



<a name="232911470"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20are%20non%20public%20items%20monomorphization%20roots%3F/near/232911470" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20are.20non.20public.20items.20monomorphization.20roots.3F.html#232911470">(Apr 02 2021 at 15:21)</a>:</h4>
<p>Yeah it's missing exported or public in there.</p>



<a name="232911533"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20are%20non%20public%20items%20monomorphization%20roots%3F/near/232911533" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20are.20non.20public.20items.20monomorphization.20roots.3F.html#232911533">(Apr 02 2021 at 15:22)</a>:</h4>
<p>We only collect those unless -Clink-dead-code is specified</p>



<a name="232920318"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20are%20non%20public%20items%20monomorphization%20roots%3F/near/232920318" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20are.20non.20public.20items.20monomorphization.20roots.3F.html#232920318">(Apr 02 2021 at 16:34)</a>:</h4>
<p>(deleted)</p>



<a name="232920493"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20are%20non%20public%20items%20monomorphization%20roots%3F/near/232920493" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20are.20non.20public.20items.20monomorphization.20roots.3F.html#232920493">(Apr 02 2021 at 16:36)</a>:</h4>
<p>I posted this as <a href="https://github.com/rust-lang/rust/issues/83787">#83787</a> (my first PR!), feel free to make additional wording suggestions there.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>